package seiacpjava_a2.utilities;

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class FileParser {

	Path filePath;
	/*
	 * This method reads a file with the following format 1,2,3,4,5,6,7,8,9,10 on one line or a file with one value on multiple lines 
	 */
	public int [] getNumbers() throws IOException {
		
		int [] items;
		List<Integer> numbers = new ArrayList<>();
		String[] lineItems = null;
		List<String> lines = Files.readAllLines(filePath,Charset.defaultCharset());
		String line;
		if (lines.size() == 1) {
			line = lines.get(0);
			lineItems = line.split(",");
			if (lineItems != null) {
				addNumbers(Arrays.asList(lineItems), numbers);
			}
		} else if (lines.size() > 1) {
			addNumbers(lines,numbers);
		}
		
		items = new int[numbers.size()];
		
		for (int i = 0 ; i < numbers.size() ; i++)
		{
			items[i] = numbers.get(i).intValue();
		}
		return items;
	}
	
	public FileParser(Path filePath) {
		super();
		this.filePath = filePath;
	}

	private void addNumbers(List<String> lines, List<Integer> numbers) {
		for(String line : lines) {
			numbers.add(Integer.valueOf(line));
		}
	}
	
}
